<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="equipType">
              <el-select placeholder="设备类型" v-model="filters.equipType" clearable>
                <el-option :value="1" label="呼叫器"></el-option>
                <el-option :value="2" label="接收器"></el-option>
                <el-option :value="99" label="安全设备"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="equipName">
              <el-input v-model="filters.equipName" clearable placeholder="设备名称"></el-input>
            </el-form-item>
            <el-form-item prop="equipCode">
              <el-input v-model="filters.equipCode" clearable placeholder="设备编号"></el-input>
            </el-form-item>
            <el-form-item prop="equipMac">
              <el-input v-model="filters.equipMac" clearable placeholder="设备MAC地址"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button v-has="permissionSendDirective" icon="el-icon-position" @click="handleSendDirevtive()" type="warning" size="mini" plain> 批量下发指令 </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table
        ref="tableList"
        @row-click="$refs.tableList.toggleRowSelection($event)"
        @selection-change="handleSelectionChange"
        :data="dataList"
        :fit="true"
        highlight-current-row
        v-loading="listLoading"
        element-loading-text="数据加载中，请稍后..."
        empty-text="对不起，没有找到相关数据"
      >
        <el-table-column type="selection" width="50" align="center"> </el-table-column>
        <el-table-column type="index" label="序号" align="center"> </el-table-column>
        <el-table-column width="200" label="设备名称" align="center" prop="equipName"> </el-table-column>
        <el-table-column width="200" label="设备编号" align="center" prop="equipCode"> </el-table-column>
        <el-table-column align="center" label="MAC地址" width="200" prop="equipMac"> </el-table-column>
        <el-table-column align="center" label="设备类型" width="110px">
          <template slot-scope="scope">
            <div v-if="scope.row.equipType === 1">呼叫器</div>
            <div v-if="scope.row.equipType === 2">接收器</div>
            <div v-if="scope.row.equipType === 99">安全设备</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="版本" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.hardware }} - {{ scope.row.software }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="设备状态" width="100px">
          <template slot-scope="scope">
            <el-tag effect="dark" size="mini" :type="formatEquipStates(scope.row.equipState)">
              {{ formatEquipState(scope.row.equipState) }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="异常状态" width="200px">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.errorState==1" effect="dark" type="danger">
              异常警告
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="设备出厂时间" prop="deliveryDate"> </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionEdit" @click="handleClickEditor(scope.row)" size="mini">开通</el-button>
            <el-button v-has="permissionDelete" @click="handleClickDelete(scope.row)" size="mini">删除</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary" @click.stop=""> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionSendDirective" @click.native="handleSendDirevtive(scope.row)"> 下发指令 </el-dropdown-item>
                <el-dropdown-item v-has="permissionSendDirective" @click.native="handleDirectiveRecord(scope.row)"> 指令记录 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
    <Directives :equipId="equipId" :beeperId="beeperId" @closeDialog="closeDirective" :dataItem="beeperData" :tabName="directiveName" :ids="selectPersonnelForm.ids" :show="directiveShow" />
  </div>
</template>

<script>
import EquipOpenPoolApi from '@/api/equip/equipOpenPool';
import * as message from '@/constants/message';
import Directives from '@/components/Directives.vue';

export default {
  name: 'EquipOpenPoolManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      equipId: 0,
      filters: {
        equipType: '',
        equipName: '',
        equipCode: '',
        equipMac: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionEdit: 'SYSTEM_MANAGER_EQUIP_EQUIPOPENPOOL_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_EQUIPOPENPOOL_LIST_DELETE',
      permissionSendDirective: 'SYSTEM_MANAGER_EQUIP_EQUIPOPENPOOL_LIST_DIRECTIVE',
      beeperId: 0,
      directiveShow: false,
      beeperData: '{}',
      selectPersonnelForm: {
        //选择人员要提交的数据
        ids: [],
      },
      directiveName: 'typeA',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {
    Directives,
  },
  methods: {
    // 下发指令
    handleSendDirevtive(row) {
      this.directiveName = 'typeA';
      if (row) {
        this.$set(row, 'beeperId', row.relationId);
        this.beeperId = row.beeperId;
        this.beeperData = JSON.stringify(row);
        this.equipId = row.equipId;
      } else {
        this.beeperData = '{}';
        if (!this.selectPersonnelForm.ids.length) return this.$message.error('请选择呼叫器');
      }
      this.directiveShow = true;
    },
    closeDirective(e) {
      this.directiveShow = e;
      this.beeperId = 0;
      this.equipId = 0;
      this.beeperData = '{}';
      this.directiveName = 'typeA';
    },
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode === 13) {
        this.onSearch();
      }
    },


    formatEquipState(cellValue) {
      if (cellValue === 0) {
        return '未开通';
      } else if (cellValue === 1) {
        return '未激活';
      } else if (cellValue === 2) {
        return '在线';
      } else if (cellValue === 3) {
        return '离线';
      }
    },
    formatEquipStates(cellValue) {
      if (cellValue === 0) {
        return 'danger';
      } else if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'info';
      }
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    resetForm() {
      this.filters.equipCode = '';
      this.filters.equipMac = '';
      this.filters.equipName = '';
      this.filters.equipType = '';
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        equipType: _this.filters.equipType,
        equipName: _this.filters.equipName,
        equipCode: _this.filters.equipCode,
        equipMac: _this.filters.equipMac,
        equipState:0
      };
      // 发送请求
      EquipOpenPoolApi.listEquip(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;

          setTimeout(() => {
            _this.$refs.tableList.doLayout();
          });
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该设备', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.equipId,
          state: -1,
        };
        EquipOpenPoolApi.deleteEquip(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickEditor(val) {
      // 编辑
      let path = '';
      if (val.equipType === 1) {
        path = '/equip/beeperEdit';
      } else if (val.equipType === 2) {
        path = '/equip/receiverEdit';
      } else if (val.equipType === 99) {
        path = '/equip/safetyEdit';
      }
      let _this = this;
      _this.$router.push({
        path: path,
        query: {
          id: val.relationId ? val.relationId : val.equipId,
          equipState: val.equipState,
        },
      });
    },
    handleSelectionChange(value) {
      this.selectPersonnelForm.ids = value.map((item) => item.relationId);
    },
    handleDirectiveRecord(row) {
      this.handleSendDirevtive(row);
      this.directiveName = 'typeB';
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
